package com.jingxuan;

import org.junit.jupiter.api.Test;
import kong.unirest.Unirest;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.json.JSONObject;

public class GetUserToken {
    @Test
    public void testGetUserToken() {
        HttpResponse<JsonNode> response = Unirest.post("http://localhost:8080/api/auth/login")
                .header("Content-Type", "application/json")
                .body("{\"phone\":\"string\",\"password\":\"string\"}")
                .asJson();

        if (response.getStatus() != 200 || response.getBody() == null) {
            throw new IllegalStateException("Login request failed: " + response.getStatus() + " - " + response.getBody());
        }

        JSONObject body = response.getBody().getObject();
        JSONObject data = body.optJSONObject("data");
        if (data == null || !data.has("token")) {
            throw new IllegalStateException("Token missing in response: " + body);
        }

        String token = data.getString("token");
        System.out.println("Token: " + token);
    }
}
